/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.achartengine.chartdemo.demo.slice;

import org.achartengine.chartdemo.demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.SeriesSelection;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;


public class PieChartBuilder extends AbilitySlice {
  /** Colors to be used for the pie slices. */
  private static int[] COLORS = new int[] { Color.GREEN.getValue(), Color.BLUE.getValue(), Color.MAGENTA.getValue(), Color.CYAN .getValue()};
  /** The main series that will include all the data. */
  private CategorySeries mSeries = new CategorySeries("");
  /** The main renderer for the main dataset. */
  private DefaultRenderer mRenderer = new DefaultRenderer();
  /** Button for adding entered data to the current series. */
  private Button mAdd;
  /** Edit text field for entering the slice value. */
  private TextField mValue;
  /** The chart view that displays the data. */
  private GraphicalView mChartView;

  @Override
  protected void onStart(Intent intent) {
    super.onStart(intent);
    setUIContent(ResourceTable.Layout_xy_chart);
  }

  @Override
  protected void onActive() {
    super.onActive();
    mValue = (TextField) findComponentById(ResourceTable.Id_xValue);
    TextField mY = (TextField) findComponentById(ResourceTable.Id_yValue);
    mY.setEnabled(false);
    ShapeElement bg = new ShapeElement(getContext(), ResourceTable.Graphic_background_text_nofield);
    mY.setBackground(bg);
    mRenderer.setZoomButtonsVisible(true);
    mRenderer.setStartAngle(180);
    mRenderer.setDisplayValues(true);

    mAdd = (Button) findComponentById(ResourceTable.Id_add);
    mAdd.setEnabled(true);
    mValue.setEnabled(true);
    mAdd.setClickedListener(new Component.ClickedListener() {
      @Override
      public void onClick(Component component) {
        double value = 0;
        try {
          value = Double.parseDouble(mValue.getText().toString());
        } catch (NumberFormatException e) {
          mValue.requestFocus();
          return;
        }
        mValue.setText("");
        mValue.requestFocus();
        mSeries.add("Series " + (mSeries.getItemCount() + 1), value);
        SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
        renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);
        mRenderer.addSeriesRenderer(renderer);
        mChartView.repaint();
      }
    });

    if (mChartView == null) {
      DirectionalLayout layout = (DirectionalLayout) findComponentById(ResourceTable.Id_chart);
      mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);
      mChartView.addDrawTask(mChartView);
      mRenderer.setClickEnabled(true);
      mChartView.setClickedListener(new Component.ClickedListener() {
        @Override
        public void onClick(Component v) {
          SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
          if (seriesSelection == null) {
            new ToastDialog(getContext())
                    .setText( "No chart element selected")
                    .show();
          } else {
            for (int i = 0; i < mSeries.getItemCount(); i++) {
              mRenderer.getSeriesRendererAt(i).setHighlighted(i == seriesSelection.getPointIndex());
            }
            mChartView.repaint();
            new ToastDialog(getContext())
                    .setText( "Chart data point index " + seriesSelection.getPointIndex() + " selected"
                            + " point value=" + seriesSelection.getValue())
                    .show();
          }
        }
      });
      layout.addComponent(mChartView, new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
              ComponentContainer.LayoutConfig.MATCH_PARENT));
    } else {
      mChartView.repaint();
    }
  }

}
